package com.photo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photo.auto.JSONAuthentication;
import com.photo.entity.PhPhoto;
import com.photo.mapper.PhPhotoMapper;
import com.photo.response.Result;
import com.photo.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author photo
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/ph-photo")
public class PhPhotoController extends JSONAuthentication {
@Autowired
    PhPhotoMapper phPhotoMapper;

    @ApiOperation(value = "添加相册")
    @PostMapping("/add")
    public void addUser(@RequestParam("file") MultipartFile file ,HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId, @RequestParam("title") String title,@RequestParam("content") String content) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        wrapper.eq("title",title);
        PhPhoto photo=new PhPhoto();
        photo.setUserId(userId);
        photo.setTitle(title);
        byte[] bytes = file.getBytes();
        String imageFileName = file.getOriginalFilename();
        String fileName = UpPhotoNameUtils.getPhotoName("img", imageFileName);
        Path path = Paths.get("D:\\自己的项目\\订餐系统\\order\\src\\assets\\picture\\" + fileName);
        //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
        Files.write(path, bytes);//写入文件
        String avatar_url = fileName;
        photo.setPicture(avatar_url);
        response.reset();
        photo.setContent(content);
        if(phPhotoMapper.selectOne(wrapper)!=null){
            Result result = Result.error().message("该用户已存在该标题相册");
            this.WriteJSON(request, response, result);
        }else{
           if( phPhotoMapper.insert(photo)>0){
               Result ok = Result.ok();
               this.WriteJSON(request, response, ok);
           }else{
               Result result = Result.error();
               this.WriteJSON(request, response, result);
           }
        }
    }
    @ApiOperation(value = "删除相册")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = phPhotoMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改相册标题")
    @PostMapping("/updPhotoTitle")
    public void updPhotoTitle(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("title") String title,@RequestParam("userId") int userId) throws Exception {
       QueryWrapper wrapper=new QueryWrapper();
       wrapper.eq("title",title);
       wrapper.eq("user_id",userId);

        if (phPhotoMapper.selectOne(wrapper) != null) {
            Result error = Result.error().message("已存在该标题相册");
            this.WriteJSON(request, response, error);
        } else {
            PhPhoto photo = phPhotoMapper.selectById(id);
            photo.setTitle(title);
            if(phPhotoMapper.updateById(photo)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "查找用户全部相册")
    @PostMapping("/findAllPhoto")
    public void findAllPhoto(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("userId") int userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<PhPhoto> page = new Page<>(current, size);
        Page<PhPhoto> pages = phPhotoMapper.selectPage(page, wrapper);
        List<PhPhoto> list = pages.getRecords();
        int total = phPhotoMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有用户");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找用户全部相册通过相册标题")
    @PostMapping("/findAllPhotoByTitle")
    public void findAllPhotoByTitle(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("userId") int userId,@RequestParam("title") String title) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        wrapper.like("title",title);
        Page<PhPhoto> page = new Page<>(current, size);
        Page<PhPhoto> pages = phPhotoMapper.selectPage(page, wrapper);
        List<PhPhoto> list = pages.getRecords();
        int total = phPhotoMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有相册");
        this.WriteJSON(request, response, error);
    }

    @ApiOperation(value = "查找全部相册")
    @PostMapping("/findPhoto")
    public void findPhoto(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<PhPhoto> page = new Page<>(current, size);
        Page<PhPhoto> pages = phPhotoMapper.selectPage(page, null);
        List<PhPhoto> list = pages.getRecords();
        int total = phPhotoMapper.selectCount(null);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有相册");
        this.WriteJSON(request, response, error);
    }


    @ApiOperation(value = "修改相册状态/ 私有 /分享")
    @PostMapping("/updPhotoStatus")
    public void updPhotoStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("status")String status) throws Exception {
        PhPhoto photo1 = phPhotoMapper.selectById(id);
        photo1.setType(status);
            if(phPhotoMapper.updateById(photo1)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }

    }
}

